package StructuralPattern.Composite.SafeMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname : Composite
 * @Description : 树枝节点
 * @Author : chentianyu
 * @Date 2023/2/19 15:38
 */


public class Composite extends Component {

    private List<Component> children;

    public Composite(String info) {
        super(info);
        this.children = new ArrayList<>();
    }

    public void addChild(Component child) {
        child.setParent(this);
        this.children.add(child);
    }

    @Override
    public void getInfo() {
        super.getInfo();
        if (this.children.isEmpty()) {
            System.out.println("3、子节点：无子节点，为叶子节点");
        } else {
            System.out.println("3、子节点：");
            for (Component child : this.children) {
                System.out.println("   " + child.info);
            }
        }
    }
}
